﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace JSON_Test
{
    internal class Program
    {
        /// <summary>
        /// 这种读取方式用的比较少，一般用来做测试，有更简单的方式读取配置文件。什么方式暂时不知道
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {

            ConfigurationBuilder configBuilder = new ConfigurationBuilder();
            
            configBuilder.AddJsonFile("config.json", optional: true, reloadOnChange: true);
            IConfigurationRoot configurationRoot = configBuilder.Build();
            
            

            /*
            //最原始的访问方式
            string name = configurationRoot["name"];
            int age = int.Parse(configurationRoot["age"]);
            string ip = configurationRoot.GetSection("proxy:ip").Value;
            int port = int.Parse(configurationRoot.GetSection("proxy:port").Value);
            Console.WriteLine($"Name: {name}, Age: {age}, Proxy IP: {ip}, Proxy Port: {port}");

            //绑定到对象
            string name2 = configurationRoot["name"];
            int age2 = int.Parse(configurationRoot["age"]);
            Proxy proxy=configurationRoot.GetSection("proxy").Get<Proxy>();
            Console.WriteLine($"Name: {name2}, Age; {age2}, Proxy IP: {proxy.Ip}, Proxy Port: {proxy.Port}");

            //直接绑定到一个对象
            Config appSettings = configurationRoot.Get<Config>();
            Console.WriteLine($"Name: {appSettings.Name}, Age: {appSettings.Age}, Proxy IP: {appSettings.Proxy.Ip}, Proxy Port: {appSettings.Proxy.Port}");

            */

            //使用选项方式读取(需要使用DI)
            ServiceCollection services = new ServiceCollection();


            //把Config对象绑定到根节点
            //好像把AddOptions()注释掉也没有影响？好像是Configure里面默认调用了AddOptions。但最好是写上，容易理解一些。
            services.AddOptions()
                .Configure<Config>(e => configurationRoot.Bind(e))
                .Configure<Proxy>(e => configurationRoot.GetSection("ProxY").Bind(e));  //这里的Proxy大小写好像无所谓
                


            services.AddScoped<TestController>();
            services.AddScoped<TestController2>();

            /*
            using (var sp = services.BuildServiceProvider())
            {
                var controller = sp.GetRequiredService<TestController>();
                controller.Test();

                Console.WriteLine("**************");

                var controller2 = sp.GetRequiredService<TestController2>();
                controller2.Test();
            }
            */

            //在运行过程中，修改配置文件
            using (var sp = services.BuildServiceProvider())
            {
                while (true)
                {
                    using (var scope = sp.CreateScope())
                    {
                        var controller = scope.ServiceProvider.GetRequiredService<TestController>();
                        controller.Test();
                        Console.WriteLine("--------TestController1---------");

                        Console.WriteLine("修改年龄");
                        Console.ReadLine();

                        controller.Test();
                        Console.WriteLine("--------TestController1---------");

                        var controller2 = sp.GetRequiredService<TestController2>();
                        controller2.Test();
                        Console.WriteLine("--------TestController2---------");
                    }

                    Console.WriteLine("点击任意键继续：");
                    Console.ReadKey();

                }
            }


        } 
    }
    public class Config
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Proxy Proxy { get; set; }
    }

    public class Proxy
    {
        public string Ip { get; set; }
        public int Port { get; set; }
    }
}
